import puppeteer from 'puppeteer';

import GoLogin from '../src/gologin.js';

const { connect } = puppeteer;

const delay = ms => new Promise(res => setTimeout(res, ms));

(async () => {
  const GL = new GoLogin({
    profile_id: '675c98e08ccc6e8186ed465e',
    token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MjhjYTJkNDQ2NDc4ZDZkMzE2Njc0NWUiLCJ0eXBlIjoiZGV2Iiwiand0aWQiOiI2MjhjYTNkYWY3ZjExMTcxMTZjZGRlNDYifQ.dGK_cQSjS1DO1AvK433tpviT6AkpYfd2tjnU4zVKERI',
    tmpdir:'C:\\Users\\Administrator\\AppData\\Local\\Temp\\'
  });

  const { status, wsUrl } = await GL.start();

  const browser = await connect({
    browserWSEndpoint: wsUrl,
    ignoreHTTPSErrors: true,
  });

  const page = await browser.newPage();
  await page.goto('https://accounts.google.com/');
  if (await page.evaluate((e) => document.querySelector('#email'))){
    // need login
    await page.type('#email', 'myemail');
    await page.type('#password', 'mypassword');
    await page.click('[type=submit]');
  }

  await page.goto('https://www.walmart.com/account/wmpurchasehistory');
  await page.screenshot({ path: 'screenshot.jpg' });
  await browser.close();
  await GL.stop();
})();
